package net.xdclass.online_xdclass.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import net.xdclass.online_xdclass.model.entity.User;

import java.util.Date;

/**
 * JWT工具类
 * @author kingtao
 * @date 2022/3/4
 */
public class JWTUtils {
    //  过期时间,一周
    static final long EXPIRE = 60000 * 60 * 24 *7;
    //  加密密钥
    private static final String SECRET = "xdclass.net168";
    //  令牌前缀
    private static final String TOKEN_PREFIX = "xdclass";
    //  subject
    private static final String SUBJECT = "xdclass";

    /**
     * 根据用户信息生成令牌
     * @author kingtao
     * @date 2022/3/4
     */
    public static String geneJsonWebToken(User user) {
        String token = Jwts.builder().setSubject(SUBJECT)
                .claim("head_img",user.getHeadImg())
                .claim("id",user.getId())
                .claim("name",user.getName())
                .setIssuedAt(new Date())
                .setExpiration( new Date(System.currentTimeMillis() + EXPIRE) )
                .signWith(SignatureAlgorithm.HS256,SECRET)
                .compact();
        token = TOKEN_PREFIX + token;
        return token;
    }
    /**
     * 校验token的方法
     * @author kingtao
     * @date 2022/3/4
     */
    public static Claims checkJWT(String token) {
        try {
            final Claims claims = Jwts.parser().setSigningKey(SECRET)
                                    .parseClaimsJws(token.replace(TOKEN_PREFIX,""))
                                    .getBody();
            return claims;
        }catch (Exception e) {
            return null;
        }
    }
}
